<?php

/**
 * 业务层基类
 */
namespace app\business;
use app\model\ModelFactory;
use app\libs\Predis;
use think\cache\driver\Redis;

class BaseBusiness {
    public $redis = null;
    //规范化ajax输出
    protected $result = array(
        'status' => 1,
        'msg' => '',
//        'msg_code'=>0,
        'data' => []
    );

    protected function error($msg = '',$status = -1,$data = []){
        $result = compact('status','msg','data');
        return $result;
    }

    protected function success($msg = '',$status = 1,$data = []){
        $result = compact('status','msg','data');
        return $result;
    }

    public function __construct()
    {
       return $this->redis = new Predis();
    }
    public function returnArr($code = 1, $msg = '', $data = '')
    {
        return array(
            //0错误，1成功，-1数据库错误，-2未登录
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        );
    }

    protected function getModel($modelName){
        return ModelFactory::getModel($modelName);
    }

    protected function getIncIdModel($modelName){
        return IncIdModelFactory::getModel($modelName);
    }


    protected function getBusiness($modelName){
        return BusinessFactory::getBusiness($modelName);
    }

    /**
     * ajax json 输出
     * array $result
     * 一般由个的元素组成'code','msg','data'
     */
    protected function ajaxReturn($result)
    {
        $this->response->setJsonContent($result);
        $this->response->send();
        $this->view->disable();
        exit();
    }

    public function sendToWebsocket($userAssets){
//        foreach ($userAssets as &$asset){
//            unset($asset['remark'],$asset['available_amount_change'],$asset['frozen_amount_change'],$asset['type'],$asset['coin_code']);
//        }
        $sendMsg['method'] = "user_assets";
        $sendMsg['user_assets'] = $userAssets;
        $this->getBusiness('WebSocket')->send($sendMsg);
    }

}
